package org.codegallery.jpagal.service.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.codegallery.jpagal.entity.Account;
import org.codegallery.jpagal.entity.Customer;
import org.codegallery.jpagal.service.AccountService;

public class AccountServiceJpaImpl implements AccountService {
	@PersistenceContext
	private EntityManager em;

	public Account save(Account account) {
		if (account.getId() == null) {
			em.persist(account);
			return account;
		} else {
			return em.merge(account);
		}
	}

	public List<Account> findByCustomer(Customer customer) {
	    TypedQuery query = em.createQuery("select a from Account a where a.customer = ?1", Account.class);
	    query.setParameter(1, customer);
	    return query.getResultList();
	}

	public void insertFoo(Account account) {
		// TODO Auto-generated method stub
		
	}

}
